package com.imti.mapper;

import com.imti.model.*;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface ExamMapper {

    @Results(id = "answer",value = {
            @Result(property = "answerId",column = "id"),
            @Result(property = "questionId",column = "question_id"),
            @Result(property = "correctFlag",column = "correct_flag")
    })
    @Select("SELECT * FROM `t_answer` where correct_flag = 1 and question_id = #{id}")
    List<Answer> findTrueAnswer(String subPart);

    @Insert("insert into t_record (paper_id,question_id,answered_id,correct_flag) " +
            "values(#{m.paper_id},#{m.question_id},#{m.answered_id},#{m.correct_flag})")
    int savePaper(Map m);

    /**
     * 查询试卷信息
     * @param paperId
     * @return
     */
    @Select("SELECT * FROM t_paper WHERE id=#{paperId}")
    @Results(id="paperResultMap",value = {
            @Result(column = "radio_count",property = "radioCount"),
            @Result(column = "radio_score",property = "radioScore"),
            @Result(column = "check_count",property = "checkCount"),
            @Result(column = "check_score",property = "checkScore"),
            @Result(column = "judge_count",property = "judgeCount"),
            @Result(column = "judge_score",property = "judgeScore")
    })
    public Paper getPaperInfo(String paperId);

    /**
     * 找出问题的所有答案
     * @return
     */
    @Select("SELECT id AS answerId,`context`,`correct_flag` AS correctFlag FROM t_answer" +
            "   WHERE `question_id`=#{questionId}")
    public List<Answer> findAllAnswerByQuestId(String questionId);

    @Select("SELECT tq.* FROM t_question tq" +
            "    WHERE tq.`id`=#{qid}")
    @Results(value = {
            @Result(column = "answer_id",property = "answerId")
    })
    public Question getQuestionByQId(String qid);

    /**
     * 保存考试记录信息
     * @param record
     * @return
     */
    @Insert("INSERT INTO `t_record`(user_id,paper_id,question_id,answered_id,correct_flag)\n" +
            "    VALUES(#{userId},#{paperId},#{questionId},#{answeredId},#{correctFlag})")
    public int saveRecode(Record record);

    @Insert("INSERT INTO  `exam_score`(user_id,exam_id,exam_time,submit_time,total_score)\n" +
            "  VALUES(#{userId},#{examId},now(),now(),#{totalScore})")
    public int saveExmScore(ExamScore examScor);
}
